package com.example.controller;

import com.example.pojo.Result;
import com.example.pojo.UserBean;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //登录接口
    @PostMapping("/login")
    public Result<UserBean> login(@RequestBody UserBean userBean, HttpServletRequest request){
        UserBean user= userService.login(userBean);
        if(user!=null){
            request.getSession().setAttribute("uid",user.getUid());
            request.getSession().setAttribute("username",user.getUsername());
        }
        return Result.ok(user,user==null?"登录失败":"登录成功",user==null?500:200);
    }
}
